package org.vincen.helloworld.jaxb;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlType(name="MapConvertor")
@XmlAccessorType(XmlAccessType.FIELD)
public class MapConverter {
	private List<MapEntry> entries = new ArrayList<MapEntry>();

	public List<MapEntry> getEntries() {
		return entries;
	}
	public void addEntry(MapEntry entry) {
		entries.add(entry);
	}

	@XmlType(name="MapEntry")
	@XmlAccessorType(XmlAccessType.FIELD)
	static class MapEntry{
		public MapEntry() {
			
		}
		//一开始使用的是Map.Entry<Object, Object>这种类型，即key和value都是Object
		//类型的，生成的xml中，就会包含如下的声明元素类型的schema信息。
		//<entries>
        //<key xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">translator</key>
        //<value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">丁雪丰</value>
        //</entries>
		public MapEntry(Map.Entry<String, String> entry) {
			this.key = entry.getKey();
			this.value = entry.getValue();
		}
		public MapEntry(String key, String value){
			this.key = key;
			this.value = value;
		}
		@XmlElement(required=true, nillable=false)
		private String key;
		private String value;
		public String getKey() {
			return key;
		}
		public void setKey(String key) {
			this.key = key;
		}
		public String getValue() {
			return value;
		}
		public void setValue(String value) {
			this.value = value;
		}
	}
}

